package com.itheima.project.vo.search;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mrchen
 * @date 2022/3/27 19:42
 */
@Data
public class QueryParam {
    public static final String QUERY_TYPE_MATCH = "match";
    public static final String QUERY_TYPE_MATCH_ALL = "match_all";
    public static final String QUERY_TYPE_TERM = "term";
    public static final String QUERY_TYPE_RANGE = "range";
    public static final String QUERY_TYPE_BOOL = "bool";
    /**
     * 查询类型
     * 如:  match  match_all term range bool
     */
    private String queryType;
    /**
     * 查询字段
     */
    private String name;
    /**
     * 查询的值   范围查询可能会有多个值
     */
    private List<String> value;
    /**
     * 范围查询类型:   lt  lte   gt  gte
     */
    private List<String> rangeType;
    /**
     * 用于 布尔查询封装must条件
     */
    private List<QueryParam> mustQuery;
    /**
     * 用于 布尔查询封装should条件
     */
    private List<QueryParam> shouldQuery;
    /**
     * 用于 布尔查询封装mustNot条件
     */
    private List<QueryParam> mustNotQuery;
    /**
     * 用于 布尔查询封装filter条件
     */
    private List<QueryParam> filterQuery;

    // =========================支持4种基础条件=============================================
    public QueryParam boolQuery(){
        this.queryType = QUERY_TYPE_BOOL;
        return this;
    }
    public QueryParam rangeQuery(String name){
        this.queryType = QUERY_TYPE_RANGE;
        this.name  = name;
        return this;
    }
    public QueryParam matchQuery(String name,String value){
        this.queryType = QUERY_TYPE_MATCH;
        addValue(value);
        this.name  = name;
        return this;
    }
    public QueryParam termQuery(String name,String value){
        this.queryType = QUERY_TYPE_TERM;
        addValue(value);
        this.name  = name;
        return this;
    }
// ======================================================================
    public QueryParam must(QueryParam queryParam){
        addMustQuery(queryParam);
        return this;
    }
    public QueryParam should(QueryParam queryParam){
        addShouldQuery(queryParam);
        return this;
    }
    public QueryParam mustNot(QueryParam queryParam){
        addMustNotQuery(queryParam);
        return this;
    }
    public QueryParam filter(QueryParam queryParam){
        addFilterQuery(queryParam);
        return this;
    }
// ===================================================================
    public QueryParam lt(String value){
        addRangeType("lt");
        addValue(value);
        return this;
    }
    public QueryParam lte(String value){
        addRangeType("lte");
        addValue(value);
        return this;
    }
    public QueryParam gt(String value){
        addRangeType("gt");
        addValue(value);
        return this;
    }
    public QueryParam gte(String value){
        addRangeType("gte");
        addValue(value);
        return this;
    }
// =====================================================
    private void addMustQuery(QueryParam queryParam){
        if(this.mustQuery == null){
            this.mustQuery = new ArrayList<>();
            this.mustQuery.add(queryParam);
        }else {
            this.mustQuery.add(queryParam);
        }
    }
    private void addMustNotQuery(QueryParam queryParam){
        if(this.mustNotQuery == null){
            this.mustNotQuery = new ArrayList<>();
            this.mustNotQuery.add(queryParam);
        }else {
            this.mustNotQuery.add(queryParam);
        }
    }
    private void addShouldQuery(QueryParam queryParam){
        if(this.shouldQuery == null){
            this.shouldQuery = new ArrayList<>();
            this.shouldQuery.add(queryParam);
        }else {
            this.shouldQuery.add(queryParam);
        }
    }
    private void addFilterQuery(QueryParam queryParam){
        if(this.filterQuery == null){
            this.filterQuery = new ArrayList<>();
            this.filterQuery.add(queryParam);
        }else {
            this.filterQuery.add(queryParam);
        }
    }
    private void addRangeType(String type){
        if(this.rangeType == null){
            this.rangeType = new ArrayList<>();
            this.rangeType.add(type);
        }else {
            this.rangeType.add(type);
        }
    }
    private void addValue(String value){
        if(this.value == null){
            this.value = new ArrayList<>();
            this.value.add(value);
        }else {
            this.value.add(value);
        }
    }
}
